﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class Info_AddRest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
           
              SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                string sql = "select * from branch";
                SqlDataAdapter da = new SqlDataAdapter(sql, con);
                DataSet ds = new DataSet();
                da.Fill(ds);
                Branch.DataSource = ds;
                Branch.DataTextField = "Branch";
                Branch.DataValueField = "Dutyid";
                Branch.DataBind();
                con.Close();
            if (Request["Employeeid"] != null)
            {
                this.MyPanel.Visible = false;     
                this.Button1.Visible = false;
                this.Button3.Visible = true;
                this.MyPanel2.Visible = false;
                this.Label2.Text = "修改员工信息";
                int Employeeid=Convert.ToInt32(Request["Employeeid"].ToString());
                SqlConnection con1 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con1.Open();
                string sql1 = "select * from Employee where Employeeid="+Employeeid+"";
                SqlCommand com1 = new SqlCommand(sql1,con1);
                SqlDataReader dr1 = com1.ExecuteReader();
                if (dr1.Read())
                {

                    name.Text = dr1["name"].ToString();
                    sex.Items.FindByValue(dr1["sex"].ToString()).Selected = true;
                    Marry.Items.FindByValue(dr1["Marry"].ToString()).Selected = true;
                    Branch.Items.FindByValue(dr1["Dutyid"].ToString()).Selected = true;
                    Birthday.Text = dr1["Birthday"].ToString();
                     Stature.Text=dr1["Stature"].ToString();
                     Avoirdupois.Text = dr1["Avoirdupois"].ToString();
                     cardtype.Text = dr1["cardtype"].ToString();
                     cardid.Text = dr1["cardid"].ToString();
                     Speciality.Text = dr1["Speciality"].ToString();
                     workPhone.Text = dr1["workPhone"].ToString();
                     movePhone.Text = dr1["movePhone"].ToString();
                     HomePhone.Text = dr1["HomePhone"].ToString();
                     xiaolingtong.Text = dr1["xiaolingtong"].ToString();
                     Homeaddress.Text = dr1["Homeaddress"].ToString();
                     Post.Text = dr1["Post"].ToString();
                     url.Text = dr1["url"].ToString();
                     Email.Text = dr1["Email"].ToString();
                     qq.Text = dr1["qq"].ToString();
                     msn.Text = dr1["msn"].ToString();

                }
                con1.Close();
            }
            else
            {
                this.MyPanel.Visible=true;
                this.Button1.Visible=true;
                this.Button3.Visible=false;
                this.Label2.Text = "添加员工";
                this.MyPanel2.Visible=true;
            }
         
        }
        
    }
    static string src = "";
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string zname = this.name.Text.Trim();

            if (username.Text.Trim() == "" || name.Text.Trim() == "")
            {
                Response.Write("<script>alert('姓名和用户名必须填写')</script>");
            }
            else
            {


                SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con.Open();
                int dutyid = Convert.ToInt32(Branch.SelectedItem.Value.ToString());
                string branch = Branch.SelectedItem.Text;
                string sexs = sex.SelectedItem.Text;
                string Marrys = Marry.SelectedItem.Text;
                string Birthdays = Birthday.Text.Trim();
                string Statures = Stature.Text.Trim();
                string Avoirdupoiss = Avoirdupois.Text.Trim();
                string cardtypes = cardtype.SelectedItem.Text;
                string cardids = cardid.Text.Trim();
                string Specialitys = Speciality.Text.Trim();
                string addTime = DateTime.Now.ToShortDateString();
                string workPhones = workPhone.Text.Trim();
                string movePhones = movePhone.Text.Trim();
                string HomePhones = HomePhone.Text.Trim();
                string xiaolingtongs = xiaolingtong.Text.Trim();
                string Homeaddresss = Homeaddress.Text.Trim();
                string Posts = Post.Text.Trim();
                string urls = url.Text.Trim();
                string Emails = Email.Text.Trim();
                string qqs = qq.Text.Trim();
                string msns = msn.Text.Trim();
                string sql = "insert into Employee values('"+username.Text.Trim()+"','" + dutyid + "','" + branch + "','" + src + "','" + zname + "','" + sexs + "','" + Marrys + "','" + Birthdays + "'," +
                    "'" + Statures + "','" + Avoirdupoiss + "','" + cardtypes + "','" + cardids + "','" + Specialitys + "','" + addTime + "','" + workPhones + "'," +
                    "'" + movePhones + "','" + HomePhones + "','" + xiaolingtongs + "','" + Homeaddresss + "','" + Posts + "','" + urls + "','" + Emails + "','" + qqs + "','" + msns + "')";
                SqlCommand com = new SqlCommand(sql, con);
                com.ExecuteNonQuery();
                SqlConnection con2 = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
                con2.Open();
                string password = "123456";
                string sql2 = "insert into userinfo values('" + this.username.Text.Trim() + "','" + password + "'," + dutyid + ")";
                SqlCommand com2 = new SqlCommand(sql2, con2);
                com2.ExecuteNonQuery();
                con2.Close();
                Response.Write("<script>alert('添加成功！');window.location.href='RestBranch.aspx';</script>");
                con.Close();
            }
        }
        catch 
        {
            Response.Write("<script>alert('数据库错误！！')</script>");
        }
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Response.Redirect("RestBranch.aspx");
    }
    protected void button2_Click(object sender, EventArgs e)
    {
        if (FileUpload1.PostedFile.ContentLength == 0)
        {
            src = "";
        }
        else
        {
            string zname = this.name.Text.Trim();
            string strFilePath = FileUpload1.PostedFile.FileName;
            FileInfo fl = new FileInfo(strFilePath);
            string Ext = fl.Extension;
            if (Ext.Equals(".jpg") || Ext.Equals(".gif") || Ext.Equals(".png") || Ext.Equals(".ico"))
            {

                src = zname + Ext;
               
                string ServerPath = Server.MapPath("~");
                string strSeraPath = ServerPath + "\\picture\\" + src;
                FileUpload1.PostedFile.SaveAs(strSeraPath);
                this.Label1.Text = "图片上传成功";
                src = zname + Ext;
            }
            else
            {
                this.Label1.Text = "图片上传格式不正确";
            }
        }
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        string names = this.name.Text;
        int dutyid = Convert.ToInt32(Branch.SelectedItem.Value.ToString());
        string branch = Branch.SelectedItem.Text;
        string sexs = sex.SelectedItem.Text;
        string Marrys = Marry.SelectedItem.Text;
        string Birthdays = Birthday.Text.Trim();
        string Statures = Stature.Text.Trim();
        string Avoirdupoiss = Avoirdupois.Text.Trim();
        string cardtypes = cardtype.SelectedItem.Text;
        string cardids = cardid.Text.Trim();
        string Specialitys = Speciality.Text.Trim();
        string addTime = DateTime.Now.ToShortDateString();
        string workPhones = workPhone.Text.Trim();
        string movePhones = movePhone.Text.Trim();
        string HomePhones = HomePhone.Text.Trim();
        string xiaolingtongs = xiaolingtong.Text.Trim();
        string Homeaddresss = Homeaddress.Text.Trim();
        string Posts = Post.Text.Trim();
        string urls = url.Text.Trim();
        string Emails = Email.Text.Trim();
        string qqs = qq.Text.Trim();
        string msns = msn.Text.Trim();
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["Connection"]);
        con.Open();
        int Employeeid = Convert.ToInt32(Request["Employeeid"].ToString());
        string sql = "update Employee set  Name='" + names + "',sex='" + sexs + "',Marry='" + Marrys + "',Birthday='" + Birthdays+ "',"+
            "Stature='" + Statures + "',branch='"+branch+"',Avoirdupois='" + Avoirdupoiss + "',cardtype='" + cardtypes + "',cardid='" + cardids + "'," +
            "Speciality='" + Specialitys + "',workPhone='" + workPhones + "',movePhone='" + movePhones + "',HomePhone='"+HomePhones+"',"+
            "xiaolingtong='" + xiaolingtongs + "',Homeaddress='" + Homeaddresss + "',Post='" + Posts + "',url='"+urls+"',"+
            "Email='" + Emails + "',qq='" + qqs + "',msn='" + msns + "' where Employeeid="+Employeeid+" ";
        SqlCommand com = new SqlCommand(sql,con);
        com.ExecuteNonQuery();
        Response.Write("<script>alert('修改成功!');window.location.href='RestBranch.aspx';</script>");
        con.Close();
    }
}
